package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"path/filepath"
	"shop/models"
	"strconv"
	"strings"
)

type ShopController struct {
	beego.Controller
}
type JsonRes struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *ShopController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "get,post")
	name := c.GetString("name")
	price, _ := c.GetFloat("price")
	img := c.GetString("img")
	intro := c.GetString("intro")
	if name == "" || price == 0 || img == "" || intro == "" {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "名称，价格，图片，描述不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	shop := models.Shop{
		Name:  name,
		Price: price,
		Img:   img,
		Intro: intro,
	}
	err := models.Add(&shop)
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *ShopController) Upload() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "get,post")
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "文件获取失败",
			Data: nil,
		}
		c.ServeJSON()
	}
	defer file.Close()
	allow := map[string]bool{
		".jpeg": true,
		".png":  true,
	}
	ext := strings.ToLower(filepath.Ext(header.Filename))
	if !allow[ext] {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "文件的类型必须为.jpeg和.png",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if header.Size > 5*1024*1024 {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "文件大小必须在5M以内",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	path := "static/img/" + header.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		imgSrc := "http://localhost:8080/" + path
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "上传成功",
			Data: imgSrc,
		}
		c.ServeJSON()
	}
}
func (c *ShopController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "get,post")
	page, _ := c.GetInt("p", 1)
	pageSize, _ := c.GetInt("s", 10)
	key := "page-" + strconv.Itoa(page) + "-" + strconv.Itoa(pageSize)
	val, err := models.Rdb.Get(key).Result()
	pageData := make(map[string]interface{})
	if err != nil {
		shop, total, err := models.Page(page, pageSize)
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
		}
		pageData["shop"] = shop
		pageData["total"] = total
		jsonByte, _ := json.Marshal(&pageData)
		models.Rdb.Set(key, string(jsonByte), 0)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "展示成功-m",
			Data: pageData,
		}
		c.ServeJSON()
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "展示成功-r",
			Data: pageData,
		}
		c.ServeJSON()
	}
}
func (c *ShopController) Show() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "get,post")
	id, _ := c.GetInt("id")
	key := "shop-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	var shop models.Shop
	if err != nil {
		err = models.DB.Model(&shop).Where("id=?", id).First(&shop).Error
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
		}
		jsonByte, _ := json.Marshal(&shop)
		models.Rdb.Set(key, string(jsonByte), 0)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "展示成功—m",
			Data: shop,
		}
		c.ServeJSON()
	} else {
		json.Unmarshal([]byte(val), &shop)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "展示成功-r",
			Data: shop,
		}
		c.ServeJSON()
	}
}
